﻿Imports System.Data.OleDb
Imports System.IO
Public Class View_Report
    Dim con As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim ds As DataSet
    Dim tname As String
    Dim tstr As String
    Dim pfooter As String
    Dim qstr As String
    Dim qcon As String
    Dim rfooter As String
    Dim qtname As String
    Dim i As Integer = 0
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        WebBrowser1.ShowPrintPreviewDialog()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        WebBrowser1.ShowPageSetupDialog()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        WebBrowser1.ShowSaveAsDialog()
    End Sub

    Public Sub fillreport(ByVal qry As String, ByVal constr As String, ByVal tablename As String)
        Try
            con = New OleDbConnection(constr)
            con.Open()
            Dim d As DataRow
            pfooter = "Demo Page Footer Text"
            Dim ad As OleDbDataAdapter
            ad = New OleDbDataAdapter(qry, con)
            ds = New DataSet
            qstr = qry
            qcon = constr
            ad.Fill(ds, "stock")
            Me.Show()
            qtname = tname
            rfooter = "Demo Report Footer Text"
            RichTextBox1.Text = RichTextBox1.Text & "<html>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<center><head>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<TITLE> REPORT </title>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<style type=" & """" & "text/css" & """" & " > " & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "@import url(" & """" & "style.css" & """" & ");" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "@media print" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "{" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "table {page-break-after:always}" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "}" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</style>"
            RichTextBox1.Text = RichTextBox1.Text & "</head></center>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<body>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<table border=1 id=" & """" & "box-table-b" & """" & ">" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<thead>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<tr>" & vbCrLf
            Dim k As Integer
            tstr = "<table border=1 id=" & """" & "box-table-b" & """" & ">"
            For i = 0 To ds.Tables(0).Columns.Count - 1
                RichTextBox1.Text = RichTextBox1.Text & "<th scope=" & """" & "col" & """" & ">" & vbCrLf
                RichTextBox1.Text = RichTextBox1.Text & ds.Tables(0).Columns(i).ColumnName & vbCrLf
                ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
                RichTextBox1.Text = RichTextBox1.Text & "</th>" & vbCrLf
            Next
            RichTextBox1.Text = RichTextBox1.Text & "<tfoot>"
            RichTextBox1.Text = RichTextBox1.Text & "<tr>"
            RichTextBox1.Text = RichTextBox1.Text & "<td colspan=" & """" & "5" & """" & "><em>" & pfooter & "</em></td>"
            RichTextBox1.Text = RichTextBox1.Text & "</tr>"
            RichTextBox1.Text = RichTextBox1.Text & "</tfoot>"
            Dim j As Integer
            Dim f As Integer
            RichTextBox1.Text = RichTextBox1.Text & "</tr>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</thead>" & vbCrLf
            For Each d In ds.Tables(0).Rows
                RichTextBox1.Text = RichTextBox1.Text & "<tr>" & vbCrLf
                For i = 0 To ds.Tables(0).Columns.Count - 1
                    RichTextBox1.Text = RichTextBox1.Text & "<td>" & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & d(i) & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & "</td>" & vbCrLf
                    j = j + 1
                Next
                If j < 50000 Then
                    RichTextBox1.Text = RichTextBox1.Text & "</tr>" & vbCrLf
                End If
                If j >= 50000 Then
                    RichTextBox1.Text = RichTextBox1.Text & "</tr>" & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & "</table>" & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & "<table border=1 id=" & """" & "box-table-b" & """" & ">" & vbCrLf
                    For f = 0 To ds.Tables(0).Columns.Count - 1
                        RichTextBox1.Text = RichTextBox1.Text & "<th>" & vbCrLf
                        RichTextBox1.Text = RichTextBox1.Text & ds.Tables(0).Columns(f).ColumnName & vbCrLf
                        ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                        ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
                        RichTextBox1.Text = RichTextBox1.Text & "</th>" & vbCrLf
                    Next
                    f = 0
                    j = 0
                End If

            Next
            If j < 50000 Then
                RichTextBox1.Text = RichTextBox1.Text & "</table>" & vbCrLf
            End If
            RichTextBox1.Text = RichTextBox1.Text & "<font color=" & """" & "#0080FF" & """" & "><Center>" & rfooter & "</center></font>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</body>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</html>"
            tname = tablename
            RichTextBox1.SaveFile(tname & ".html", RichTextBoxStreamType.PlainText)
            WebBrowser1.Navigate(Application.StartupPath & "\" & tname & ".html")
            RichTextBox1.Visible = False
            '   RichTextBox1.Visible = False
            'Dim k As Integer
            'For i = 0 To ds.Tables(0).Columns.Count - 1
            '    ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
            '    ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
            'Next
            ds.Clear()
            i = 1
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub View_Report_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            My.Computer.FileSystem.DeleteFile(tname & ".html")
            My.Computer.FileSystem.DeleteFile("closing.html")
            My.Computer.FileSystem.DeleteFile("logo.png")
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub View_Report_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbtemplate.Text = "Template 1"
    End Sub

    Private Sub cmbtemplate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbtemplate.SelectedIndexChanged
        If i <> 0 Then
            If cmbtemplate.Text = "Template 1" Then
                filltemplate("<table border=1 id=" & """" & "box-table-b" & """" & ">")
            ElseIf cmbtemplate.Text = "Template 2" Then
                filltemplate("<table border=1 id=" & """" & "ver-minimalist" & """" & ">")
            ElseIf cmbtemplate.Text = "Template 3" Then
                filltemplate("<table border=1 id=" & """" & "hor-zebra" & """" & ">")
            ElseIf cmbtemplate.Text = "Template 4" Then
                filltemplate("<table border=1 id=" & """" & "ver-zebra" & """" & ">")
            ElseIf cmbtemplate.Text = "Template 5" Then
                filltemplate("<table border=1 id=" & """" & "one-column-emphasis" & """" & ">")
            ElseIf cmbtemplate.Text = "Template 6" Then
                filltemplate("<table border=1 id=" & """" & "newspaper-a" & """" & ">")
            ElseIf cmbtemplate.Text = "Template 7" Then
                filltemplate("<table border=1 id=" & """" & "newspaper-b" & """" & ">")
            ElseIf cmbtemplate.Text = "Template 8" Then
                filltemplate("<table border=1 id=" & """" & "newspaper-c" & """" & ">")
            End If
        End If
    End Sub

    Private Sub cmbtemplate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbtemplate.TextChanged
       
    End Sub
    Private Sub filltemplate(ByVal str As String)
        RichTextBox1.Text = RichTextBox1.Text.Replace(tstr, str)
        tstr = str
        RichTextBox1.SaveFile(tname & ".html", RichTextBoxStreamType.PlainText)
        WebBrowser1.Navigate(Application.StartupPath & "\" & tname & ".html")
        RichTextBox1.Visible = False
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Dim str As String
        str = InputBox("Enter Page Footer", "Page Footer", pfooter)
        RichTextBox1.Text = RichTextBox1.Text.Replace("<tfoot><tr><td colspan=" & """" & "5" & """" & "><em>" & pfooter & "</em></td></tr></tfoot>", "<tfoot><tr><td colspan=" & """" & "5" & """" & "><em>" & str & "</em></td></tr></tfoot>")
        pfooter = str
        str = InputBox("Enter Report Footer", "Report Footer", rfooter)
        RichTextBox1.Text = RichTextBox1.Text.Replace("<font color=" & """" & "#0080FF" & """" & "><Center>" & rfooter & "</center></font>", "<font color=" & """" & "#0080FF" & """" & "><Center>" & str & "</center></font>")
        rfooter = str
        RichTextBox1.SaveFile(tname & ".html", RichTextBoxStreamType.PlainText)
        WebBrowser1.Navigate(Application.StartupPath & "\" & tname & ".html")
        RichTextBox1.Visible = False
    End Sub
    Private Sub fillplain()
        Try
            Dim str As String
            con = New OleDbConnection(qcon)
            RichTextBox1.Text = ""
            con.Open()
            Dim d As DataRow
            pfooter = "Demo Page Footer Text"
            Dim ad As OleDbDataAdapter
            ad = New OleDbDataAdapter(qstr, con)
            ds = New DataSet
            ad.Fill(ds, "stock")
            Me.Show()
            rfooter = "Demo Report Footer Text"
            RichTextBox1.Text = RichTextBox1.Text & "<html>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<center><head>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<TITLE> REPORT </title>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</head></center>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<body>" & vbCrLf
            str = InputBox("Enter Page Footer", "Page Footer", pfooter)
            pfooter = str
            RichTextBox1.Text = RichTextBox1.Text & "<center><h1>" & str & "</h1></center>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<center>"
            RichTextBox1.Text = RichTextBox1.Text & "<table border=1>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<thead>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "<tr>" & vbCrLf
            Dim k As Integer
            tstr = "<table border=1>"
            For i = 0 To ds.Tables(0).Columns.Count - 1
                RichTextBox1.Text = RichTextBox1.Text & "<th scope=" & """" & "col" & """" & ">" & vbCrLf
                RichTextBox1.Text = RichTextBox1.Text & ds.Tables(0).Columns(i).ColumnName & vbCrLf
                ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
                ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
                RichTextBox1.Text = RichTextBox1.Text & "</th>" & vbCrLf
            Next
            'RichTextBox1.Text = RichTextBox1.Text & "<tfoot>"
            'RichTextBox1.Text = RichTextBox1.Text & "<tr>"
            'RichTextBox1.Text = RichTextBox1.Text & "<td colspan=" & """" & "5" & """" & "><em>" & pfooter & "</em></td>"
            'RichTextBox1.Text = RichTextBox1.Text & "</tr>"
            'RichTextBox1.Text = RichTextBox1.Text & "</tfoot>"
            Dim j As Integer
            Dim f As Integer
            RichTextBox1.Text = RichTextBox1.Text & "</tr>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</thead>" & vbCrLf
            For Each d In ds.Tables(0).Rows
                RichTextBox1.Text = RichTextBox1.Text & "<tr>" & vbCrLf
                For i = 0 To ds.Tables(0).Columns.Count - 1
                    RichTextBox1.Text = RichTextBox1.Text & "<td>" & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & d(i) & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & "</td>" & vbCrLf
                    j = j + 1
                Next
                If j < 50000 Then
                    RichTextBox1.Text = RichTextBox1.Text & "</tr>" & vbCrLf
                End If
                If j >= 50000 Then
                    RichTextBox1.Text = RichTextBox1.Text & "</tr>" & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & "</table>" & vbCrLf
                    RichTextBox1.Text = RichTextBox1.Text & "<table border=1>" & vbCrLf
                    For f = 0 To ds.Tables(0).Columns.Count - 1
                        RichTextBox1.Text = RichTextBox1.Text & "<th>" & vbCrLf
                        RichTextBox1.Text = RichTextBox1.Text & ds.Tables(0).Columns(f).ColumnName & vbCrLf
                        RichTextBox1.Text = RichTextBox1.Text & "</th>" & vbCrLf
                    Next
                    f = 0
                    j = 0
                End If

            Next
            If j < 50000 Then
                RichTextBox1.Text = RichTextBox1.Text & "</table>" & vbCrLf
            End If
            RichTextBox1.Text = RichTextBox1.Text & "</center>"
            str = InputBox("Enter Report Footer", "Report Footer", rfooter)
            rfooter = str
            RichTextBox1.Text = RichTextBox1.Text & "<font><Center>" & str & "</center></font>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</body>" & vbCrLf
            RichTextBox1.Text = RichTextBox1.Text & "</html>"
            RichTextBox1.SaveFile(qtname & ".html", RichTextBoxStreamType.PlainText)
            WebBrowser1.Navigate(Application.StartupPath & "\" & qtname & ".html")
            RichTextBox1.Visible = False
            '   RichTextBox1.Visible = False
            'Dim k As Integer
            'For i = 0 To ds.Tables(0).Columns.Count - 1
            '    ' TreeView1.Nodes(j).Nodes.Add(ds.Tables(0).Columns(i).ToString)
            '    ' TreeView1.Nodes(j).Nodes(i).Nodes.Add(ds.Tables(0).Columns(i).DataType.ToString.Substring(InStrRev(ds.Tables(0).Columns(i).DataType.ToString, ".")))
            'Next
            ds.Clear()
            i = 1
            con.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        fillplain()
        cmbtemplate.Enabled = False
        ToolStripButton4.Enabled = False
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        Custom_Template.fillreport(RichTextBox1, qstr, qcon, tname, WebBrowser1)
    End Sub
End Class